$(function(){

	//加载选择医院信息
	$.getJSON("http://gh.com/home/yiyuan",{type:"yiyuan"},function(response){
		$("select[name='yiyuan']").empty();
		if(response.result){
			$("select[name='yiyuan']").append("<option value='no'>请选择医院</option>");
			$.each(response.data.list,function(k,v){
				$("select[name='yiyuan']").append("<option value="+v.id+">"+v.name+"</option>");
			})
		}
	})

	$("select[name='yiyuan']").on("change",function(){	
		var data={};
		data.type="ksid";
		data.domain=window.location.host;
		/*
		data.yiyuanid=$("select[name='yiyuan']").val();

		if(data.yiyuanid=="no"||data.yiyuanid==""){
			$("select[name='ksid']").hide();
			$("select[name='ksid']").empty();
			return;
		}*/

		$.getJSON("http://gh.com/home/yiyuan",data,function(response){
			if(response.result){
				$("select[name='ksid']").empty();
				$.each(response.data.list,function(i,vi){					
					$("select[name='ksid']").append("<option value="+vi.id+">"+vi.name+"</option>");
				})			
				$("select[name='ksid']").show();
			}
			
		});
	})

	$("input[name='btn']").on("click",function(){
		var data={};
		data.username=$("input[name='gh_name']").val();
		data.age=$("input[name='gh_age']").val();
		data.sex=$("input[name='gh_sex']").val();
		data.phone=$("input[name='gh_tel']").val();
		data.yytime=$("input[name='gh_date']").val();
		data.yiyuan=$("select[name='yiyuan']").val();
		data.ksid=$("select[name='ksid']").val();
		data.address=$("input[name='address']").val();
		data.remark=$("textarea[name='gh_des']").val();
		//data.domain=window.location.host;

		if(data.username==""){
			alert("请填写用户名");
			return;
		}

		if(data.age!=""){
			if(isNaN(data.age)){
				alert("请输入年龄");
				return;
			}
		}	
		
		if(data.phone==""){
			alert("请填写手机号");
			return;
		}else{
			if(isNaN(data.phone)){
				alert("请填写正确的手机号");
				return;
			}else{
				if(data.phone.length!=11){
					alert("请输入正确的手机号");
					return;
				}
			}
		}

		if(data.yytime==""){
			alert("请选择预约时间");
			return;
		}

		if(data.yiyuan==""||data.yiyuan=="no"){
			alert("请选择医院");
			return;
		}

		if(data.ksid==""){
			alert("请选择科室");
			return;
		}

		$.getJSON("http://gh.com/home/guahaoyy",data,function(response){
			if(response.result){
				alert(response.msg);
			}else{
				alert(response.msg);
			}
		})

	})
})